package com.xyoto.community.recover;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.constant.FollowStatus;
import com.xyoto.community.entity.main.FanFollow;
import com.xyoto.community.mapper.main.FanFollowMapper;
import com.xyoto.community.utils.RedisCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Author: 廖振辉
 * Date: 2022/1/16
 * Description: 同步数据库中的用户关注状态到redis
 */
public class FansFollowRecover {

    @Autowired
    private FanFollowMapper fanFollowMapper;

    @Autowired
    private RedisCommonUtils redisCommonUtils;


    public void recoverFansFollow(){
        List<FanFollow> fanFollowList = fanFollowMapper.selectAllByFollowStatus(FollowStatus.FOLLOW);
        for (FanFollow fanFollow : fanFollowList) {
            Integer userId = fanFollow.getUserId();
            Integer targetId = fanFollow.getTargetId();
            redisCommonUtils.setBit(CacheInstruction.USER_FOLLOW+userId,targetId,true);
        }
        List<FanFollow> unFanFollowsList = fanFollowMapper.selectAllByFollowStatus(FollowStatus.UN_FOLLOW);
        for (FanFollow unFanFollow : unFanFollowsList) {
            Integer userId = unFanFollow.getUserId();
            Integer targetId = unFanFollow.getTargetId();
            redisCommonUtils.setBit(CacheInstruction.USER_FOLLOW+userId,targetId,false);
        }
    }
}
